<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useUserStore } from '@/stores/useUserStore'

const userStore = useUserStore()

onMounted(() => {
  userStore.restoreFromLocalStorage()
})
// 钩子函数获取路由实例
const route = useRoute()
const activeTab = ref('首页')
onMounted(() => {
  const storedTab = localStorage.getItem('activeTab')
  if (storedTab) {
    activeTab.value = storedTab
  } else {
    // 如果没有存储的值，根据路由设置 activeTab
    switch (route.path) {
      case '/':
        activeTab.value = '首页'
        break
      case '/classification':
        activeTab.value = '分类'
        break
      case '/shoppingcart':
        activeTab.value = '购物车'
        break
      case '/my':
        activeTab.value = '我的'
        break
      default:
        activeTab.value = '首页' // 默认值
    }
  }
})

// 监听 activeTab 的变化并存储到 localStorage
watch(activeTab, (newValue) => {
  localStorage.setItem('activeTab', newValue)
})
</script>

<template>
  <header>
    <div class="wrapper">
      <nav v-show="route.meta.isshowbar">
        <div class="tab" :class="{ tab_active: activeTab === '首页' }" @click="activeTab = '首页'">
          <router-link to="/">
            <div class="img">
              <div
                class="bg_img"
                :style="{
                  backgroundImage:
                    activeTab === '首页'
                      ? `url(https://mall.leyifan.com/static/h5/new_images/home_active.png)`
                      : `url(https://mall.leyifan.com/static/h5/new_images/home.png)`,
                }"
              ></div>
            </div>
            首页</router-link
          >
        </div>
        <div class="tab" :class="{ tab_active: activeTab === '分类' }" @click="activeTab = '分类'">
          <router-link to="/classification">
            <div class="img">
              <div
                class="bg_img"
                :style="{
                  backgroundImage:
                    activeTab === '分类'
                      ? `url(https://mall.leyifan.com/static/h5/new_images/site_active.png)`
                      : `url(https://mall.leyifan.com/static/h5/new_images/site.png)`,
                }"
              ></div>
            </div>
            分类
          </router-link>
        </div>
        <div
          class="tab"
          :class="{ tab_active: activeTab === '购物车' }"
          @click="activeTab = '购物车'"
        >
          <router-link to="/shoppingcart">
            <div class="img">
              <div
                class="bg_img"
                :style="{
                  backgroundImage:
                    activeTab === '购物车'
                      ? `url(https://mall.leyifan.com/static/h5/new_images/shopping_active.png)`
                      : `url(https://mall.leyifan.com/static/h5/new_images/shopping.png)`,
                }"
              ></div>
            </div>
            购物车
          </router-link>
        </div>
        <div class="tab" :class="{ tab_active: activeTab === '我的' }" @click="activeTab = '我的'">
          <router-link to="/my">
            <div class="img">
              <div
                class="bg_img"
                :style="{
                  backgroundImage:
                    activeTab === '我的'
                      ? `url(https://mall.leyifan.com/static/h5/new_images/my_active.png)`
                      : `url(https://mall.leyifan.com/static/h5/new_images/my.png)`,
                }"
              ></div>
            </div>
            我的
          </router-link>
        </div>
      </nav>
    </div>
  </header>

  <RouterView />
</template>

<style lang="less" scoped>
#app {
  color: #2c3e50;
  font-size: 16rem;
  padding: 0 !important;
}
a {
  display: flex;
  flex-direction: column;
  align-items: center;
}
nav {
  background-color: #fff;
  border-top: 1rem solid #f5f5f5;
  z-index: 15;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100vw;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding-bottom: env(safe-area-inset-bottom);
  height: calc(49rem + env(safe-area-inset-bottom));
  border-top: 1rem solid #f5f5f5;
  .tab {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #333;
    font-size: 10rem;
    width: 50rem;
    .img {
      width: 26rem;
      height: 26rem;
      max-width: 26rem;
      max-height: 26rem;
      min-width: 26rem;
      min-height: 26rem;
      .bg_img {
        background-image: url(./assets/home.png);
        background-position: 0% 0%;
        background-size: 100% 100%;
        background-repeat: no-repeat;
        width: 100%;
        height: 100%;
      }
    }
  }
  .tab_active {
    width: 26rem;
    height: 26rem;
    max-height: 26rem;
    min-width: 26rem;
    min-height: 26rem;
    width: 50rem;
  }

  a {
    font-weight: bold;
    color: #2c3e50;
    text-decoration: none;
    font-size: 10rem;
    width: 100%;
    text-align: center;
    &.router-link-exact-active {
      color: #ff3d58;
      font-weight: 700;
    }
  }
}
</style>
